<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/25
 * Time: 下午2:13
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class RuleController extends AdminBaseController{
    /**
     * 权限列表
     */
    public function index(){
        $this -> display();
    }
    public function pageList(){
        $page = I('get.page/d',1);

        $rule = M('rule');
        //  总记录数
        $count = $rule -> count();
        $limit = 20;
        //  总页数
        $pages = ceil($count/$limit);
        $start = empty($page - 1) ? 0 : (($page - 1) * $limit);

        $list = $rule -> order('id') -> limit($start . ',' . $limit) -> select();

        $list = get_tree($list, 0, 0, '----&nbsp;');
        $content = '';
        foreach($list as $value){
            $content .= '<tr><td>' . $value['id'] . '</td><td>' . $value['html'] . $value['title'] . '</td><td>' . $value['name'] . '</td><td>';
            if($value['status']){
                $content .= '<a href="javascript:statusChange(' . $value['id'] . ');" class="btn btn-outline btn-sm btn-primary" id="status' . $value['id'] . '">正常</a>';
            }else{
                $content .= '<a href="javascript:statusChange(' . $value['id'] . ');" class="btn btn-outline btn-sm btn-danger" id="status' . $value['id'] . '">禁用</a>';
            }
            $content .= '</td><td><div class="col-md-12"><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/Rule/add/pid/' .$value['id']) . '"><i class="fa fa-edit"></i> 添加子权限</a><a class="btn btn-sm btn-outline btn-warning" href="' . U('Admin/Rule/edit/id/' .$value['id']) . '"><i class="fa fa-edit"></i> 修改</a><a class="btn btn-sm btn-outline btn-danger" href="javascript:del(' . $value['id'] . ')"><i class="fa fa-trash-o"></i> 删除</a></div></td></tr>';
        }
        $data = [
            'pages' => $pages,
            'content' => $content,
        ];
        echo json_encode($data);
    }

    /**
     * 权限添加表单
     */
    public function add(){
        $pid = I('get.pid/d',0);
        $ruleInfo =[
            'status'=>1,
            'pid' => $pid
        ];
        $this->assign('ruleInfo',$ruleInfo);
        $this -> display();
    }
    /**
     * 权限修改表单(与添加共用了同一个模版文件)
     */
    public function edit(){
        if(IS_GET){
            $id = I("get.id/d",0);
            if(!$id){
                $this->error('非法操作','/Admin/Rule/index');
            }
            $ruleInfo = M('Rule')->field('id,pid,name,title,status')->where(['id'=>$id])->find();

            $this->assign('ruleInfo',$ruleInfo);
            $this->display('Rule_add');
        }
    }

    /**
     * 修改及保存
     */
    public function Save(){
        if(IS_AJAX && IS_POST){
            $id = I('post.id/d',0);
            $title = I("post.title/s",'');
            if(!$title){
                $this->ajaxReturn(['status' => 1, 'msg' => '权限名称不能为空！']);
            }
            $titleArr = strpos($title,',') === false ? false : explode(',',$title);
            $name = I("post.name/s",'');
            if(!$name){
                $this->ajaxReturn(['status' => 1, 'msg' => '权限不能为空！']);
            }
            $nameArr = strpos($name,',') === false ? false : explode(',',$name);
            $status = I("post.status/d",0);
            $pid = I("post.pid/d",0);
            $strSql = '';
            $rule = M('Rule');

            /**
             *  id为空是添加 否则为修改
             *  $titleArr 和 $nameArr对应 支持批量添加
             */
            if($titleArr && $nameArr && empty($id)){
                foreach($nameArr as $key => $val){
                    $strSql .= '('.$pid.',"'.$val.'","'.$titleArr[$key].'",'.$status.'),';
                }
                $strSql = rtrim($strSql,',');
                $sql = 'insert into dx_rule(pid,name,title,status) VALUES '.$strSql;
                $isSQl = $rule -> execute($sql);
            }else{
                $ruleNum = $rule->where(['title'=>$title,'name'=>$name])->count();
                if($ruleNum > 0 && empty($id)){
                    $this->ajaxReturn(['status' => 1, 'msg' => '权限已经存在！']);
                }

                $data = [
                    'id' => $id,
                    'pid' => $pid,
                    'name'=> $name,
                    'title'=> $title,
                    'status'=> $status
                ];

                $isSQl = $rule -> add($data,[],true);
            }
            if(!$isSQl){
                $this->ajaxReturn(['status' => 1, 'msg' => '失败！']);
            }
            $this->ajaxReturn(['status' => 0, 'msg' => '成功！']);
        }else{
            $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
        }
    }

    /**
     * 更改权限状态
     */
    public function statusChange(){
        if(IS_AJAX && IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
            }
            $rule = M('Rule');
            $r = $rule ->field('status') -> where(['id' => $id]) -> find();
            if(1 == $r['status']){
                $rule -> where(['id' => $id]) -> setField('status', 0);
                $this -> ajaxReturn(['status' => 0, 'msg' => '已禁用', 'data' => 0]);
            }else{
                $rule -> where(['id' => $id]) -> setField('status', 1);
                $this -> ajaxReturn(['status' => 0, 'msg' => '已开启', 'data' => 1]);
            }
        }else{
            $this -> ajaxReturn(['status' => 1, 'msg' => '非法操作']);
        }
    }

    /**
     * 删除权限
     */
    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
            }
            //  查询是否有子权限
            $ruleCount = M('Rule')->where(['pid' => $id])->count();
            if($ruleCount > 0){
                $this->ajaxReturn(['status' => 1 , 'msg' => '请先删除子权限！']);
            }
            $isDel = M('Rule')->where(['id' => $id])->delete();
            if(!$isDel){
                $this->ajaxReturn(['status' => 1 , 'msg' => '删除失败！']);
            }
            $this->ajaxReturn(['status' => 0 , 'msg' => '删除成功！']);
        }else{
            $this->ajaxReturn(['status' => 1 , 'msg' => '非法操作!']);
        }
    }
}